package com.ydgk.dao;

import com.ydgk.bean.Student;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import java.util.List;

public class StudentDao extends BaseDao<Student> {
    @Override
    public int save(Student student) {
        return template.update("insert into student values(?,?,?,?,?)",
                student.getSid(),
                student.getSname(),
                student.getSsex(),
                student.getSage(),
                student.getTid());
    }

    @Override
    public int updateById(Student student) {
        return template.update("update student set sname = ?,ssex = ?,sage = ?,tid = ? where sid = ?",
                student.getSname(),
                student.getSsex(),
                student.getSage(),
                student.getTid(),
                student.getSid());
    }

    @Override
    public int deleteById(int id) {
        try {
            return template.update("delete from student where sid = ?", id);
        }catch (Exception e){
            return -1;
        }
    }

    @Override
    public List<Student> findAll() {
        return template.query("select *from student",new BeanPropertyRowMapper<Student>(Student.class));
    }

    @Override
    public Student findById(int id) {
        String sql = "select * from student where sid =?" ;
        List<Student> list = template.query(sql, new BeanPropertyRowMapper<Student>(Student.class),id);
        return list.size() > 0 ? list.get(0) : null ;
    }

    @Override
    public List<Student> paging(int pageNumber) {
        return template.query("select * from student order by sid desc limit ?,?",new BeanPropertyRowMapper<Student>(Student.class) , (pageNumber-1)*pageCount ,pageCount);
    }

    public int total() {
        return template.queryForObject("select count(*) from student",Integer.class);
    }
}
